/******************************************************************
 * Created by: GOH CHEE HONG                                     
 * Date of Creation: 1/AUG/2008     
 * Date of last modification: 
 *                                                                
 * Description: PROVIDE A MEAN TO GENERATE A KEY PAIR
 ******************************************************************/
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Pbdms.Common.CryptoUtils;

namespace Pbdms.Forms.Tools
{
    public partial class FrmKeyPair :Form
    {
        private Boolean firstTime=true;
        public FrmKeyPair()
        {
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
          
        }
        /// <summary>
        /// check if this is the starting screen
        /// </summary>
        public void setNotFirstTime()
        {
            firstTime = false;
        }
        /// <summary>
        /// generate key button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGen_Click(object sender, EventArgs e)
        {
            string password=null;
            try
            {   
                if(txtid.Text==null || txtid.Text.Equals(""))
                {
                    throw new Exception("Please enter your name");
                }
                if (txtemail.Text == null || txtemail.Text.Equals(""))
                {
                    throw new Exception("Please enter your email");
                }
                if(txtpass.Text!=null || !txtpass.Text.Equals(""))
                {
                       if (!(txtpass.Text.Equals(txtrepass.Text)))
                       {
                           throw new Exception("Passwords do not match");
                       }
                       else
                       {
                           password=txtpass.Text;
                       }
                }
                if (txtpass.Text.Equals(""))
                {
                    throw new Exception("require a password");
                }
                string allid = "<"+txtid.Text+"><"+txtemail.Text+">";

                if (!(txtcomment.Text.Equals("")))
                {
                    allid=allid+"<" +txtcomment.Text+">";
                }
                RsaKeyRingGenerator gen = new RsaKeyRingGenerator(allid,password.ToCharArray());
                gen.GenerateKeyPair();
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                
                //PgpPublicKeyManager pubManager = new PgpPublicKeyManager();
                
                //pubManager.AddAPublicKey(gen.getPublicKey());

                SecretKeyManager secManager = new SecretKeyManager();
                secManager.replaceSecretKey(gen.getSecretKey());
                secManager.close();
                

                Program.keyControl.setSecretKey(gen.getSecretKey());
                Program.keyControl.setPrivateKey(gen.getSecretKey().ExtractPrivateKey(password.ToCharArray()));



                    File.Delete(Common.Global.getTempFolder() + "\\MessageCache.dat");
                    if (firstTime == false)
                    {
                        foreach (BusinessLogic.PeerMS.PeerTracker peerTracker in Program.mainForm.frmBuddiesStatus.peerTrackingManager.HPeerTrackers.Values)
                        {
                            peerTracker.setTrusted(false);
                            Program.mainForm.frmBuddiesStatus.moveTreeNode(peerTracker.ID, "all", "untrusted");
                            
                            Program.friendControl.setABuddyTrustworthy(peerTracker.ID, false);
                            //set all untrusted
                            Program.mainForm.frmKeyManager.setALlUntrusted();
                            
                            //Program.mainForm.frmBuddiesStatus.btnSignPub.Text = "Trust A Friend";
                        }
                        //clear all messages
                        Program.mainForm.frmInbox.clear();
                        
                    }
                MessageBox.Show("Key Pair Created");
                
                this.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// cancel button event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}